import PropManager from "./PropManager";
import GameManager from "../common/GameManager";
import AudioManager from "../common/AudioManager";
import Facebook from "../Facebook/Facebook";
import Gmbox from "../gmbox/Gmbox";

// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class OpenTrasureChest extends cc.Component {

    @property([cc.Node])
    stars: cc.Node[] = [];

    @property(cc.Node)
    treasure: cc.Node;

    @property(cc.Node)
    light: cc.Node;

    @property(cc.Node)
    prop_2048: cc.Node;

    @property(cc.Node)
    prop_finger: cc.Node;

    @property(cc.Node)
    prop_hammer: cc.Node;

    @property(cc.Button)
    continueBtn: cc.Button;

    start() {
    }

    update(dt) {

    }

    onEnable() {
        let audioManager: AudioManager = GameManager.getInstance().audioManager;
        audioManager.playEffect("tanchubaoxiang");
        this.treasure.y = 500;
        this.treasure.runAction(cc.moveTo(0.5, cc.v2(0, 0)).easing(cc.easeBounceOut()));
    }


    /* public async openOld() {
        var anim: cc.Animation;
        let self = this;
        this.stars.forEach(element => {
            element.destroy();
        });

        this.light.destroy();
        anim = this.treasure.getComponent(cc.Animation)
        anim.play();
        setTimeout(() => {
            let audioManager: AudioManager = GameManager.getInstance().audioManager;
            audioManager.playEffect("prop");

            let propType: number = PropManager.getInstance().ranAddProp();
            let propNode: cc.Node;
            if (propType == 0) {
                propNode = this.prop_2048;
            } else if (propType == 1) {
                propNode = this.prop_finger;
            } else {
                propNode = this.prop_hammer;
            }
            propNode.active = true;
            anim = propNode.getComponent(cc.Animation)
            // anim.on('finished', () => {
            // });
            anim.play();
            GameManager.getInstance().eventCenter.DispatchEvent("getProp", {
                type: propType,
                node: propNode,
                pos: self.node.convertToWorldSpaceAR(propNode.position),
                cb: () => {
                    PropManager.getInstance().changeProp(propType, 1);
                },
                fade: () => {
                    self.node.runAction(cc.sequence(cc.fadeOut(3.3), cc.callFunc(() => {

                        self.node.destroy();
                    })));
                }
            })
        }, 300);
        this.continueBtn.node.active = false;
    } */

    public async close() {
        this.node.destroy();
        if (Gmbox.RewardedVideoAd) {
            Gmbox.RewardedVideoAd.abandonShow();
        }
    }

    public async open() {
        let rewards = [
            {
                prob: 0.40,
                type: 1,
                count: 1
            },
            {
                prob: 0.40,
                type: 2,
                count: 1
            },
            {
                prob: 0.10,
                type: 1,
                count: 2
            }, {
                prob: 0.10,
                type: 2,
                count: 2
            }
        ];
        let probabilities: number[] = new Array(rewards.length);
        for (let i = 0; i < rewards.length; i++) {
            probabilities[i] = rewards[i].prob;
        }
        let reward = rewards[PropManager.getInstance().getRandomProp(probabilities)];
        PropManager.getInstance().openPropLayer(reward.type, reward.count)
        this.node.destroy();
        if (Gmbox.RewardedVideoAd) {
            Gmbox.RewardedVideoAd.abandonShow();
        }
    }



    public async openAdBox() {
        let successCallback = () => {
            let rewards = [
                {
                    prob: 0.25,
                    type: 1,
                    count: 1
                },
                {
                    prob: 0.25,
                    type: 2,
                    count: 1
                },
                {
                    prob: 0.20,
                    type: 1,
                    count: 2
                }, {
                    prob: 0.20,
                    type: 2,
                    count: 2
                }, {
                    prob: 0.05,
                    type: 1,
                    count: 3
                }, {
                    prob: 0.05,
                    type: 2,
                    count: 3
                }
            ];
            let probabilities: number[] = new Array(rewards.length);
            for (let i = 0; i < rewards.length; i++) {
                probabilities[i] = rewards[i].prob;
            }
            let reward = rewards[PropManager.getInstance().getRandomProp(probabilities)];
            PropManager.getInstance().openPropLayer(reward.type, reward.count)
            this.node.destroy();
        }
        if (Gmbox.RewardedVideoAd) {
            Gmbox.RewardedVideoAd.showRewardedVideoAd(successCallback);
        } else {
            Facebook.FBTrasureChestAd.showRewardedVideoAd(successCallback);
        }
    }

    public get(): void {

    }
}
